﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;



namespace expander
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /*
         * @brief : 在字符串增加空格
         * @input : str 源字符串
         *          index 添加空格符的间隔位置
         * @return: 增加空格的字符串
         * @date  : 2015-04-26
         */
        public string add_space(string str, int index)
        {
            StringBuilder sb = new StringBuilder(str);
            for (int i = index; i < sb.Length; i += index + 1)
            {
                sb.Insert(i, " ");
            }
            return sb.ToString();
        } 

        public UInt16 calc_crc16(Byte[] buf, int len)
        {
            UInt16 crc = 0xffff;
            int i_byte = 0, i_bit = 0;

            for (i_byte=0; i_byte<len; i_byte++)
            {
                crc = (UInt16)(crc ^ buf[i_byte]);
                for(i_bit = 0; i_bit < 8; i_bit++)
                {
                    byte t = 0;
                    t = (byte)(crc & 0x0001);
                    crc = (UInt16)(crc >> 1);
                    crc = (UInt16)(crc & 0x7fff);
                    if(t > 0)
                    {
                        crc = (UInt16)(crc ^ 0xa001);
                    }
                }
            }

            return crc;
        }

        class CRC32Cls
        {
            protected ulong[] Crc32Table;         //生成CRC32码表   

            public void GetCRC32Table()
            {
                ulong Crc;
                Crc32Table = new ulong[256];

                for (int i = 0; i < 256; i++)
                {
                    Crc = (ulong)i;

                    for (int j = 8; j > 0; j--)
                    {
                        if ((Crc & 1) == 1)
                            Crc = (Crc >> 1) ^ 0xEDB88320;
                        else
                            Crc >>= 1;
                    }

                    Crc32Table[i] = Crc;
                }
            }

            public ulong GetCRC32Str(byte[] src_data) //获取字符串的CRC32校验值   
            {
                GetCRC32Table();  //生成码表 
                ulong value = 0xffffffff;

                for (int i = 0; i < src_data.Length; i++)
                {
                    value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ src_data[i]];
                }

                return value ^ 0xffffffff;
            }
        }

        private void set_line_no()
        {
            textblock_1.Text = "";
            int start_index = textbox_5.GetFirstVisibleLineIndex();
            int end_index = textbox_5.GetLastVisibleLineIndex();

            for (int i = start_index; i < end_index+1; i++)/**显示行号*/
            {
                textblock_1.Text += (i + 1).ToString() + "\r\n";
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textbox_5.Text.IndexOf(" ") >= 0)/**查找数据内容是否有空格符*/
            {
                textbox_5.Text = textbox_5.Text.Replace("\r\n", ""); /**删除换行符*/
                textbox_5.Text = textbox_5.Text.Replace(" ", "");    /**删除空格符*/
            }
            else
            {
                textbox_5.Text = add_space(textbox_5.Text, 2);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            /**清屏*/
            textbox_2.Text = "";
            textbox_3.Text = "";
            textbox_4.Text = "";
            textbox_5.Text = "";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            UInt16 crc16;
            int i, j=0,sum = 0, index;
            Byte[] buf = new Byte[textbox_5.Text.Length];

            if (textbox_5.Text.Length == 0)
            {
                MessageBox.Show("请输入被校验的数据！");
                return;
            }

            if (textbox_5.Text.IndexOf(" ") >= 0)
                index = 3;
            else
                index = 2;

            i = int.Parse(textbox_1.Text) * index;
            if (i >= textbox_5.Text.Length)
            {
                MessageBox.Show("输入的起始位错误！");
                return;
            }

            for (; i < textbox_5.Text.Length; i += index)
            {
                try
                {
                    if (textbox_5.Text.Length - i == 1)
                    {
                         buf[j++] = Convert.ToByte(textbox_5.Text.Substring(i, 1), 16);
                    }
                    else
                    {
                         buf[j++] = Convert.ToByte(textbox_5.Text.Substring(i, 2), 16);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                
                sum += buf[j - 1];
            }

            textbox_2.Text = "0x" + sum.ToString("X");

            crc16 = calc_crc16(buf, j);
            textbox_3.Text = "0x" + crc16.ToString("X");

            CRC32Cls crc32 = new CRC32Cls();
            buf = System.Text.Encoding.ASCII.GetBytes(textbox_5.Text);
            textbox_4.Text = "0x" + crc32.GetCRC32Str(buf).ToString("X");
        }

        private void textbox_5_TextChanged(object sender, TextChangedEventArgs e)
        {
            set_line_no();
            //textblock_1.Text = "";
            //for (int i = 0; i < textbox_5.LineCount; i++)/**显示行号*/
            //{
            //    textblock_1.Text += (i + 1).ToString() + "\r\n";
            //}
            //scroll_viewer.ScrollToVerticalOffset(textbox_5.VerticalOffset);
            //textbox_4.Text = textbox_5.VerticalOffset.ToString();

            //textbox_3.Text = textbox_5.GetLastVisibleLineIndex().ToString();
            //textbox_4.Text = textbox_5.GetFirstVisibleLineIndex().ToString();
        }

        private void textbox_5_MouseWheel(object sender, MouseWheelEventArgs e)/**滚动鼠标改动实现字体缩放*/
        {
            set_line_no();

            if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control)/**检测是否按下ctrl键*/
                return;

            if (e.Delta > 0)
            {
                textbox_5.FontSize += 1;
                if (textbox_5.FontSize > 72)
                    textbox_5.FontSize = 72;
            }
            else
            {
                textbox_5.FontSize -= 1;
                if (textbox_5.FontSize < 8)
                    textbox_5.FontSize = 8;
            }

            textblock_1.FontSize = textbox_5.FontSize; 
        }

        //private void textbox_key(object sender, KeyEventArgs e)
        //{
        //    set_line_no();
        //}

        //private void textbox_mouse(object sender, MouseButtonEventArgs e)
        //{
        //    set_line_no();
        //}

        private void textbox_5_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            set_line_no();
        }
    }
}
